/*
 * @Date         : 2024-05-11 19:47:14 星期6
 * @Author       : xut
 * @Description  :
 */
const grpc = require("@grpc/grpc-js")
const messages = require("./gen/helloworld_pb")
const services = require("./gen/helloworld_grpc_pb")

function sayHello(call, callback) {
  const reply = new messages.HelloReply()
  reply.setMessage("Hello " + call.request.getName())
  callback(null, reply)
}

function main() {
  const server = new grpc.Server()
  server.addService(services.GreeterService, { sayHello: sayHello })
  server.bindAsync(
    "localhost:50051",
    grpc.ServerCredentials.createInsecure(),
    (err, port) => {
      if (err != null) {
        return console.error(err)
      }
      console.log(`gRPC listening on ${port}`)
    }
  )
}

main()
